package com.chnbst.bstyun.controller.admin;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chnbst.bstyun.model.pojo.GoodsDownload;
import com.chnbst.bstyun.model.pojo.GoodsDownloadCenter;
import com.chnbst.bstyun.model.vo.GoodsDownloadVo;
import com.chnbst.bstyun.service.GoodsDownloadCenterService;
import com.chnbst.bstyun.service.GoodsDownloadService;

/**
 * 控制器注解 请求映射注解
 * controller 代表类是控制器
 * restcontroller 代表类是控制器， 所有的方法 都是ajax方法
 * @author WangXinLei
 *
 */
@Controller
@RequestMapping("/sysAdmin")
public class DownLoadAdminControlller {
	
	@Autowired
	private GoodsDownloadService goodsDownloadService;
	@Autowired
	private GoodsDownloadCenterService goodsDownloadCenterService;
	
	/**
	 * 下载中心
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/downAdmin/downManager",method=RequestMethod.GET)
	public ModelAndView downManager()throws Exception{
		List<GoodsDownloadVo> goodsDownloadList = goodsDownloadService.selectGrids();
		List<GoodsDownloadCenter> goodsDownloadCenterList = goodsDownloadCenterService.selectGrid();

		for(int i=0;i<goodsDownloadList.size();i++){
			GoodsDownloadVo goodsDownloadVo = goodsDownloadList.get(i);
			goodsDownloadVo.setIsAndroid(1);
			goodsDownloadVo.setIsIos(1);
			goodsDownloadVo.setIsPc(1);
			goodsDownloadVo.setIsInk(1);
			String goodsDownloadId = goodsDownloadVo.getGoodsDownloadId();
			for(int j=0;j<goodsDownloadCenterList.size();j++){
				GoodsDownloadCenter  goodsDownloadCenter = goodsDownloadCenterList.get(j);
				String centerId = goodsDownloadCenter.getGoodsDownloadId();
				int wayId = goodsDownloadCenter.getDownloadWayId();
				if(goodsDownloadId.equals(centerId)){
					if(wayId == 1){
						goodsDownloadVo.setIsAndroid(0);
					}else if(wayId == 2){
						goodsDownloadVo.setIsIos(0);
					}else if(wayId == 3){
						goodsDownloadVo.setIsPc(0);
					}else if(wayId == 4){
						goodsDownloadVo.setIsInk(0);
					}
				}
			}
		}
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/admin/systemManager/downManager");
		mav.addObject("goodsDownloadList", goodsDownloadList);
		return mav;
	}
	
	/**
	 * 新增界面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/downAdmin/addDownloadLink",method=RequestMethod.GET)
	public ModelAndView addDownloadLink()throws Exception{
		return new ModelAndView("/admin/systemManager/addDownloadLink");
	}
	
	/**
	 * @return 新增
	 * @throws UnsupportedEncodingException 
	 * @throws Exception
	 */
	@RequestMapping(value="/operate/downAdmin/saveGoodsDownload",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> saveGoodsDownload(String downloadId,String decoration,String name,String img,
			String apk,String apkqrcode,String deskurl,String ios,String iosqrcode,
			String pcexe,String procode,String slogan,String adapter,HttpServletRequest request)throws Exception{
			
		boolean isSuccess = false;
		GoodsDownload newGoodsDownload = new GoodsDownload();
		GoodsDownload goodsDownload = new GoodsDownload();
		goodsDownload.setGoodsDownloadId(downloadId);
		goodsDownload.setCreateTime(new Date());
		goodsDownload.setGoodsDownloadDescribe(decoration);
		goodsDownload.setGoodsDownloadName(name);
		goodsDownload.setImageUrl(img);
		goodsDownload.setIsDelete(0);
		goodsDownload.setGoodsCode(procode);
		goodsDownload.setSlogan(slogan);
		goodsDownload.setAdapter(adapter);
		
		if("".equals(goodsDownload.getGoodsDownloadId())){
			isSuccess = goodsDownloadService.insertGoodsDownload(goodsDownload);
			newGoodsDownload = goodsDownloadService.selectByIdNum(goodsDownload.getId());
			downloadId = newGoodsDownload.getGoodsDownloadId();
		}else{
			isSuccess = goodsDownloadService.updateById(goodsDownload);
		}
		
		List<GoodsDownloadCenter> goodsCenterList = goodsDownloadCenterService.selectGridByDownloadId(downloadId);
		for(int i=0;i<goodsCenterList.size();i++){
			goodsDownloadCenterService.deleteById(goodsCenterList.get(i).getDownloadCenterId());
		}
		
		GoodsDownloadCenter goodsDownloadCenter = new GoodsDownloadCenter();
		goodsDownloadCenter.setGoodsDownloadId(downloadId);
		if(!"".equals(apk)){
			goodsDownloadCenter.setDownloadWayId(1);
			goodsDownloadCenter.setDownloadUrl(apk);
			goodsDownloadCenter.setDownloadImageUrl(apkqrcode);
			goodsDownloadCenterService.insertGoodsDownloadCenter(goodsDownloadCenter);
		}
		if(!"".equals(ios)){
			goodsDownloadCenter.setDownloadWayId(2);
			goodsDownloadCenter.setDownloadUrl(ios);
			goodsDownloadCenter.setDownloadImageUrl(iosqrcode);
			goodsDownloadCenterService.insertGoodsDownloadCenter(goodsDownloadCenter);
		}
		if(!"".equals(pcexe)){
			goodsDownloadCenter.setDownloadWayId(3);
			goodsDownloadCenter.setDownloadUrl(pcexe);
			goodsDownloadCenter.setDownloadImageUrl("");
			goodsDownloadCenterService.insertGoodsDownloadCenter(goodsDownloadCenter);
		}
		if(!"".equals(deskurl)){
			goodsDownloadCenter.setDownloadWayId(4);
			goodsDownloadCenter.setDownloadUrl(deskurl);
			goodsDownloadCenter.setDownloadImageUrl("");
			goodsDownloadCenterService.insertGoodsDownloadCenter(goodsDownloadCenter);
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isSuccess", isSuccess);
		
		return map;
		
	}
	
	/**
	 * @return 根据ID 查询
	 * @throws UnsupportedEncodingException 
	 * @throws Exception
	 */
	@RequestMapping(value="/downAdmin/selectById/{goodsDownloadId}",method=RequestMethod.GET)
	public ModelAndView selectById(@PathVariable String goodsDownloadId,HttpServletRequest request)throws Exception{
		
		GoodsDownloadVo goodsDownload = goodsDownloadService.selectById(goodsDownloadId);
		List<GoodsDownloadCenter> goodsDownloadCenterList = goodsDownloadCenterService.selectGridByDownloadId(goodsDownloadId);

		goodsDownload.setIsAndroid(1);
		goodsDownload.setAndroidUrl("");
		goodsDownload.setIsIos(1);
		goodsDownload.setIosUrl("");
		goodsDownload.setIsPc(1);
		goodsDownload.setPcUrl("");
		goodsDownload.setIsInk(1);
		goodsDownload.setInkUrl("");
		
		
		for(int j=0;j<goodsDownloadCenterList.size();j++){
			GoodsDownloadCenter  goodsDownloadCenter = goodsDownloadCenterList.get(j);
			int wayId = goodsDownloadCenter.getDownloadWayId();
			String downUrl = goodsDownloadCenter.getDownloadUrl();
			String imageUrl = goodsDownloadCenter.getDownloadImageUrl();
			if(wayId == 1){
				goodsDownload.setIsAndroid(0);
				goodsDownload.setAndroidUrl(downUrl);
				goodsDownload.setAndroidImageUrl(imageUrl);
			}else if(wayId == 2){
				goodsDownload.setIsIos(0);
				goodsDownload.setIosUrl(downUrl);
				goodsDownload.setIosImageUrl(imageUrl);
			}else if(wayId == 3){
				goodsDownload.setIsPc(0);
				goodsDownload.setPcUrl(downUrl);
			}else if(wayId == 4){
				goodsDownload.setIsInk(0);
				goodsDownload.setInkUrl(downUrl);
			}
		}
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/admin/systemManager/addDownloadLink");
		mav.addObject("goodsDownload", goodsDownload);
		return mav;
	}

	/**
	 * @return 根据ID 删除 (单条删除和批量删除)
	 * @throws UnsupportedEncodingException 
	 * @throws Exception
	 */
	@RequestMapping(value="/operate/downAdmin/deleteById",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteById(String ids,HttpServletRequest request)throws Exception{
		
		boolean isDelete = false;
		
		String[] goodsDownloadList = ids.split(",");
		for(int i=0;i<goodsDownloadList.length;i++){
			GoodsDownload goodsDownload = goodsDownloadService.selectById(goodsDownloadList[i]);
			goodsDownload.setIsDelete(1);
			isDelete = goodsDownloadService.updateById(goodsDownload);
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isDelete", isDelete);
		
		return map;
		
	}
	
}
